#include "laguagecontroller.h"
#include <QSettings>
#include <QDir>
#include <QApplication>


LaguageController *LaguageController::m_LanguageController = NULL;
LaguageController::LaguageController(QObject *parent)
    : QObject{parent}
{
    m_translator = new QTranslator(this);
}


LaguageController *LaguageController::getInstance()
{
    if(!m_LanguageController)
        m_LanguageController = new LaguageController();
    return m_LanguageController;
}

void LaguageController::setCurrentLanguage(E_LANGUAGE language)
{
    switch(language)
    {
    case English_E:
        if(m_translator->load("./SituationalInteractiveStand_EN.qm"))
            qApp->installTranslator(m_translator);
        break;
    case Chinese_E:
        if(m_translator->load("./SituationalInteractiveStand_CN.qm"))
            qApp->installTranslator(m_translator);
        break;
    }
    m_language = language;
    //    emit signalLanguageChanged(m_language);
    QString dirPath =   "./conf/";
    QDir confdir(dirPath);

    if(!confdir.exists())
        confdir.mkdir(dirPath);
    QString confFile(dirPath + "IDconf.ini");
    QSettings iniSetting(confFile, QSettings::IniFormat);
    iniSetting.setValue("language",language);
}

E_LANGUAGE LaguageController::getCurrentLanguage()
{
    return m_language;
}

E_LANGUAGE LaguageController::getConfigLanguage()
{
    QString dirPath = QApplication::applicationDirPath() + "/conf/";
    QDir confdir(dirPath);
    if(!confdir.exists())
        confdir.mkdir(dirPath);
    QString confFile(dirPath + "IDconf.ini");
    QSettings iniSetting(confFile, QSettings::IniFormat);
    int value = iniSetting.value("language").toUInt();
    m_language = E_LANGUAGE(value);
    return m_language;
}
